package com.redoor.app.stake.basicdata.timetask;

import com.redoor.app.stake.basicdata.model.*;
import com.redoor.app.stake.basicdata.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * Created by wangjunlei on 2018/2/24.
 */
@Configuration
@EnableScheduling
public class LogSchedulingConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private LogService logService;
    @Autowired
    private BaseHsfOperationLogService baseHsfOperationLogService;
    @Autowired
    private BaseMqOperationLogService baseMqOperationLogService;
    @Autowired
    private BaseAssetOperationTrajectoryService baseAssetOperationTrajectoryService;
    @Autowired
    private BaseAssetOperationDetailLogService baseAssetOperationDetailLogService;

    @Autowired
    private BaseSystemOperationLogService baseSystemOperationLogService;

    /**
     * 定时获取redis中的待处理数据，批量写入到数据库中
     */
    @Scheduled(fixedRate = 60000)
    public void dealLogIntoDataBase() {
        logger.info("dealLogIntoDataBase begin");
        try {
            List<BaseHsfOperationLog> logList = logService.getLogFromRedis(10, LogEnum.HSF_LOG);
            if(logList!=null && logList.size() > 0){
                int result = baseHsfOperationLogService.saveAll(logList);
                if(result > 0) {
                    logger.info("insert hsf log success");
                }
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }

        List<BaseMqOperationLog> mqLogList = logService.getLogFromRedis(10, LogEnum.MQ_LOG);
        if(mqLogList!=null && mqLogList.size() > 0){
            int result = baseMqOperationLogService.saveAll(mqLogList);
            if(result > 0) {
                logger.info("insert mq log success");
            }
        }

        logger.info("dealLogIntoDataBase end");
    }

    /**
     * 定时获取redis中的待处理数据，批量写入到数据库中
     */
    @Scheduled(fixedRate = 70000)
    public void dealHSFLogIntoDataBase() {
        logger.info("dealHSFLogIntoDataBase begin");
        try {
            List<BaseAssetOperationTrajectory> baseAssetOperationTrajectories = logService.getLogFromRedis(10, LogEnum.ASSET_TRACE_LOG);
            if(baseAssetOperationTrajectories!=null && baseAssetOperationTrajectories.size() > 0){
                int result = baseAssetOperationTrajectoryService.saveAll(baseAssetOperationTrajectories);
                if(result > 0) {
                    logger.info("insert trace log success");
                }
            }

            List<BaseAssetOperationDetailLog> baseAssetOperationDetailLogs = logService.getLogFromRedis(10, LogEnum.ASSET_TRACE_DETAIL_LOG);
            if(baseAssetOperationDetailLogs!=null && baseAssetOperationDetailLogs.size() > 0){
                int result = baseAssetOperationDetailLogService.saveAll(baseAssetOperationDetailLogs);
                if(result > 0) {
                    logger.info("insert trace detail log success");
                }
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        logger.info("dealHSFLogIntoDataBase end");
    }

    /**
     * 定时获取redis中的待处理数据，批量写入到数据库中
     */
    @Scheduled(fixedRate = 70000)
    public void dealSystemLogIntoDataBase() {
        logger.info("dealSystemLogIntoDataBase begin");
        try {
            List<BaseSystemOperationLog> logList = logService.getLogFromRedis(10, LogEnum.WEB_LOG);
            if(logList!=null && logList.size() > 0) {
                int result = baseSystemOperationLogService.saveAll(logList);
                if(result > 0) {
                    logger.info("insert web log success");
                }
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        logger.info("dealSystemLogIntoDataBase end");
    }
}
